package main

import (
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"log"
	"time"
	"xorm.io/builder"
)

func GroupHandler(app *mvc.Application) {
	// app.Register(...)
	// app.Router.Use/UseGlobal/Done(...)
	app.Handle(new(GroupController))
}

type GroupController struct{}

func Pagination(ctx iris.Context) (limit, offset int) {
	page := ctx.URLParamIntDefault("page", 0)
	size := ctx.URLParamIntDefault("size", 20)
	return size, page * size
}

// Get 返回企微群列表
func (c *GroupController) Get(ctx iris.Context) {
	var request Group
	if err := ctx.ReadQuery(&request); err != nil {
		log.Fatalln(err)
		return
	}
	session := engine.Limit(Pagination(ctx))
	if request.Name != "" {
		session.Where(builder.Like{"name", request.Name})
	}
	if request.OwnerName != "" {
		session.Where(builder.Like{"owner_name", request.OwnerName})
	}
	if request.StoreCode != "" {
		session.Where(builder.Like{"store_code", request.StoreCode})
	}
	if request.StoreName != "" {
		session.Where(builder.Like{"store_name", request.StoreName})
	}

	var groups []Group
	if err := session.Find(&groups); err != nil {
		log.Fatalln(err)
		return
	}

	_, _ = ctx.JSON(Success(groups))
}

// Post 添加群信息
func (c *GroupController) Post(ctx iris.Context) {
	var group Group
	if err := ctx.ReadJSON(&group); err != nil {
		_, _ = ctx.JSON(Fail(err))
		return
	}

	inserted, err := engine.Insert(group)
	if err != nil {
		return
	}
	_, _ = ctx.JSON(Success(inserted))
}

// Put 更新群信息
func (c *GroupController) Put(ctx iris.Context) {
	var group Group
	if err := ctx.ReadJSON(&group); err != nil {
		_, _ = ctx.JSON(Fail(err))
		return
	}

	valid(&group)

	updated, err := engine.ID(group.ChatId).UseBool().Update(&group)
	if err != nil {
		log.Fatalln(err)
		return
	}
	_, _ = ctx.JSON(Success(updated))
}

func (c *GroupController) Delete(ctx iris.Context) (int64, error) {
	var group Group
	err := ctx.ReadQuery(&group)
	if err != nil {
		return 0, nil
	}

	return engine.ID(group.ChatId).Delete(&group)
}

func valid(group *Group) bool {
	println(group)
	return true
}

// Group 企微群
type Group struct {
	ChatId      string    `xorm:"pk varchar(50) not null comment('聊天群ID')" json:"chatId"`
	Name        string    `xorm:"varchar(50) not null comment('群名称')" json:"name"`
	OwnerId     string    `xorm:"varchar(50) comment('群主ID')" json:"ownerId"`
	OwnerName   string    `xorm:"varchar(50) comment('群主名称')" json:"ownerName"`
	Status      int       `xorm:"comment('状态，0 - 跟进人正常，1 - 跟进人离职，2 - 离职继承中，3 - 离职继承完成')" json:"status"`
	MemberCount int       `xorm:"comment('群成员数量')" json:"memberCount"`
	StoreId     int64     `xorm:"bigint comment('门店ID')" json:"storeId"`
	StoreCode   string    `xorm:"varchar(50) comment('门店编码')" json:"storeCode"`
	StoreName   string    `xorm:"varchar(50) comment('门店名称')" json:"storeName"`
	StoreStatus int       `xorm:"comment('门店状态，0 - 正常')" json:"storeStatus"`
	Version     int       `xorm:"version default(1) comment('门店状态')" json:"version"`
	Created     time.Time `xorm:"created default(CURRENT_TIMESTAMP) comment('创建时间')" json:"created" time_format:"2006-01-02 15:04:05"`
	Updated     time.Time `xorm:"updated default(CURRENT_TIMESTAMP) comment('最后更新时间')" json:"updated" time_format:"2006-01-02 15:04:05"`
	Deleted     time.Time `xorm:"deleted comment('删除时间')" json:"deleted" time_format:"2006-01-02 15:04:05"`
}

// GroupUser 群成员
type GroupUser struct {
	Id      int64     `xorm:"pk autoincr comment('ID')"`
	ChatId  string    `xorm:"varchar(50) comment('客户群ID')"`
	UserId  string    `xorm:"varchar(50) comment('群成员ID')"`
	UnionId string    `xorm:"varchar(50) comment('微信UnionID')"`
	Type    int       `xorm:"comment('类型，0 - 企业成员，1 - 外部联系人')"`
	Level   int       `xorm:"comment('级别，0 - 成员，1 - 管理员')"`
	Status  int       `xorm:"comment('状态')"`
	Created time.Time `xorm:"created default(CURRENT_TIMESTAMP) comment('创建时间')" time_format:"2006-01-02 15:04:05"`
	Updated time.Time `xorm:"updated default(CURRENT_TIMESTAMP) comment('最后更新时间')" time_format:"2006-01-02 15:04:05"`
}
